home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / Magic Oracle / Fortune.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  3.1 KB  |  90 lines  |  [TEXT/SNJ2]

  1. /**
  2.  *  Apple Worldwide Developer Technical Support
  3.  *
  4.  *  Sample demonstrating AppleScript for Java.
  5.  *
  6.  *  by Michael Hopkins, Apple Developer Technical Support
  7.  *
  8.  *  File:   Fortune.java
  9.  *
  10.  *  Copyright ©1999 Apple Computer, Inc.
  11.  *  All rights reserved.
  12.  *
  13.  *    4/99    v. 1.0    Shipped as 'Magic Oracle AppleScript for Java' sample.
  14.  *
  15.  *  You may incorporate this sample code into your applications without
  16.  *  restriction, though the sample code has been provided "AS IS" and the
  17.  *  responsibility for its operation is 100% yours.  However, what you are
  18.  *  not permitted to do is to redistribute the source as "Apple Sample
  19.  *  Code" after having made changes. If you're going to re-distribute the
  20.  *  source, we require that you make it clear in the source that the code
  21.  *  was descended from Apple Sample Code, but that you've made changes.
  22. **/
  23.  
  24. import java.util.Vector;
  25. import java.util.Random;
  26.  
  27.  
  28. /**
  29.  * A class by the Magic Oracle application used to retrieve string responses to questions
  30.  * It is important that the order of the elements and the strings match the order and 
  31.  * contents of the images used to display the fortunes
  32.  * @version 1.0, April 9, 1999
  33.  * @author Michael Hopkins, Worldwide Developer Technical Support, Apple Computer Inc.
  34.  **/
  35.  
  36. public class Fortune extends java.lang.Object
  37. {
  38.     public Fortune()
  39.     {
  40.         fortunes.addElement( "Answer hazy, try back later." );  // image5
  41.         fortunes.addElement( "Ask again later." );                // image6
  42.         fortunes.addElement( "It is hard to say." );            // image7
  43.         fortunes.addElement( "It doesn't look good." );            // image8
  44.         fortunes.addElement( "My sources say No." );            // image9
  45.         fortunes.addElement( "Probably not." );                    // image10
  46.         fortunes.addElement( "Outlook not so good." );            // image11
  47.         fortunes.addElement( "Don't count on it." );            // image12
  48.         fortunes.addElement( "NO" );                            // image13
  49.         fortunes.addElement( "Who cares?" );                    // image14
  50.         fortunes.addElement( "It appears to be." );                // image15
  51.         fortunes.addElement( "Signs point to yes." );            // image16
  52.         fortunes.addElement( "YES." );                            // image17
  53.         fortunes.addElement( "Count on it." );                    // image18
  54.         fortunes.addElement( "Definately so." );                // image19
  55.         fortunes.addElement( "It is the case." );                // image20
  56.         fortunes.addElement( "It could be." );                    // image21
  57.         fortunes.addElement( "Outlook good." );                    // image22
  58.         fortunes.addElement( "Sources say yes." );                // image23
  59.         fortunes.addElement( "Signs point to no." );            // image24
  60.     }
  61.     
  62.     /**
  63.      * Generates a response number between 1 and the number of responses 
  64.      * (entry into reponse vector)
  65.      * @return integer response number 
  66.      */
  67.     public int generateResponseNum()
  68.     {
  69.         double temp = generator.nextDouble();
  70.         int size = fortunes.size();
  71.         int result = (int) ((temp * 100) % size);
  72.         response = (String) fortunes.elementAt( result );
  73.         return result;
  74.     }
  75.     
  76.     /**
  77.      * Generates a response as a string based on the generated response number
  78.      * @return String response as a string 
  79.      */
  80.     public String getResponse()
  81.     {
  82.         return response;
  83.     }
  84.     
  85.     protected Vector fortunes = new Vector();
  86.     protected Random generator= new Random();
  87.     protected String response= "";
  88.     
  89. }
  90.